package fr.gadcparis.envieparisienne.model;

import java.util.ArrayList;
import java.util.List;

import android.location.Location;

public class ODSMetaClient implements ODSClientInterface {
	
	private List<ODSClientInterface> clients;
	
	public ODSMetaClient() {
		this.clients = new ArrayList<ODSClientInterface>();
	}
	
	public ODSMetaClient addClient(ODSClientInterface client) {
		this.clients.add(client);
		return this;
	}

	public ODSClientInterface addFilter(String filter) {
		for (ODSClientInterface client: this.clients) {
			client.addFilter(filter);
		}
		return this;
	}

	public ODSClientInterface near(Location location) {
		for (ODSClientInterface client: this.clients) {
			client.near(location);
		}
		return this;
	}

	public ODSClientInterface near(Location location, long distance) {
		for (ODSClientInterface client: this.clients) {
			client.near(location, distance);
		}
		return this;
	}

	public int count() {
		int count = 0;
		for (ODSClientInterface client: this.clients) {
			count += client.count();
		}
		return count;
	}

	public Hit getHit(int i) {
		if (i < 0) {
			return null;
		}
		
		int count = 0;
		for (ODSClientInterface client: this.clients) {
			if (i < count + client.count()) {
				return client.getHit(i - count);
			}
			count += client.count();
		}
		
		return null;
	}

	public ODSClientInterface limit(int limit) {
		for (ODSClientInterface client: this.clients) {
			client.limit(limit);
		}
		return this;
	}

}
